//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientSetupHostage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_freed_msg","_die_msg","_shared_free","_shared_alive","_free_code","_die_code","_evt_idx","_action_id","_act_id"
		,"_obj_var_freed","_is_freed","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);

if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);
	if (_object isKindOf "Man") then {
		_freed_msg = [_parameters, "FREED_MSG", ""] call CFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", ""] call CFUNC(hashGet);
		_shared_free = [_parameters, "SHARED_FREE_VAR", ""] call CFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", ""] call CFUNC(hashGet);
		_free_code = [_parameters, "FREE_CODE", ""] call CFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", ""] call CFUNC(hashGet);
		
		_act_id = [_object,_action,"HOSTAGE_ACTION",-1] call FFUNC(actionGetLocal);
		if (_act_id >= 0) then {
			TRACE_1("Remove Action",_act_id);
			_object removeAction _act_id;
		};
		
		_obj_var_freed = format["mission_person_freed_%1",_action_id];
		_is_freed = OGETVAR(_object,_obj_var_freed,false);
		If (isNil "_is_freed") then {
			_is_freed = false;
		};
		if (!_is_freed) then {
			_object setCaptive true;
			_action_title = ("STR_MF_RESCUE_ACTION") call CFUNC(translate);
			_arguments = [];
			_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, _action_id];
			_action_display_timer = QGVAR(action_display_timer);
			_object setVariable [_action_display_timer,0];
			_condition = format["(alive _target) && (_target getVariable '%1' < time)",_action_display_timer];
			_act_id = _object addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,10,True,True,"",_condition];
			// We set the variable as local because it is dependent on the computer running it.
			[_object,_action,"HOSTAGE_ACTION",_act_id] call FFUNC(actionSetLocal);
			TRACE_1("Added Action",ARR_4(_act_id,_action_title,typeof _object,_condition));
			if (alive _object) then {
				// The following code will play the captive animation until the hostage is freed or dead.
				_object spawn {
					private ["_hostage","_curr_anim"];
					_hostage = _this;
					TRACE_2("Start",_hostage, (_hostage isKindOf "Woman"));
					While {captive _hostage && alive _hostage} do {
						_move = format["CivilSitting0%1",(floor(random(3))+1)];
						// Women need another animation.
						if (_hostage isKindOf "Woman") then {_move = format["amovpercmstpslowwrfldnon_amovpsitmstpsraswrfldnon",(floor(random(3))+1)];};
						_hostage switchMove _move;
						// _animstate = animationState _hostage;
						waituntil {"amovpercmstpslowwrfldnon_amovpsitmstpsraswrfldnon" != animationState _hostage};
						sleep 1.234;
					};
					TRACE_2("Start",_hostage, alive _hostage);
					if (alive _hostage) then {
						_hostage switchMove "SitStandUp";
					};
				};
			};
		};
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;